import nattyFetch from "natty-fetch";
import {
  urlPrefix,
  isDev,
  apiUrlPrefixProd,
  geoApiUrlPrefix,
  elasticSearchServer,
} from "./variables";
import StorageUtils from "./storage";

const context = nattyFetch.context({
  urlPrefix: apiUrlPrefixProd,
  mockUrlPrefix: urlPrefix,
  mock: false,
  // jsonp: true,
  withCredentials: false,
  traditional: true,
  method: "POST",
  postDataFormat: "JSON",
  timeout: 60000,
  fit(response) {
    let ret = {
      success: response.success,
    };
    if (ret.success) {
      if (response.hasOwnProperty("data")) {
        ret.content = response.data;
      } else {
        ret.content = null;
      }
    } else {
      ret.error = {
        message: response.msg,
      };
    }
    return ret;
  },
});
//系统登录
context.create("sys", {
  auth: {
    url: "auth",
    method: "POST",
    header: {
      "Content-Type": "application/json",
    },
    fit(response) {
      let ret = {
        success: response.success,
      };
      if (ret.success) {
        if (response.hasOwnProperty("data")) {
          ret.content = response.data;
          StorageUtils.setToken(response.data.token);
        } else {
          ret.content = null;
        }
      } else {
        ret.error = {
          message: response.msg,
        };
      }
      return ret;
    },
  },
  refresh: {
    method: "GET",
    url: "refresh",
    fit(response) {
      let ret = {
        success: response.success,
      };
      if (ret.success) {
        if (response.hasOwnProperty("data")) {
          ret.content = response.data;
          StorageUtils.setToken(response.data.token);
        } else {
          ret.content = null;
        }
      } else {
        ret.error = {
          message: response.msg,
        };
      }
      return ret;
    },
  },
  resource: {
    method: "GET",
    url: "indexData",
  },
});
// 基础数据
context.create("basic.info", {
  // 企业/人员基础信息查询
  oeq: {
    url: "sskj/basic/oeq",
  },
  // 企业类型比例查询
  oc: {
    url: "sskj/basic/oc",
  },
  // 招标中工程项目数量查询,在建工程项目数量查询,竣工工程项目数量查询
  gcec: {
    url: "sskj/basic/gcec",
  },
  // 人员证书分类数据查询
  pcc: {
    url: "sskj/basic/pcc",
  },
  // 人员证书分类数据查询（全部）
  pccall: {
    url: "sskj/basic/pccall",
  },
  // 企业信用评级数据查询
  ocr: {
    url: "sskj/basic/ocr",
  },
  // 个人信用评级数据查询
  pcr: {
    url: "sskj/basic/pcr",
  },
  pg: {
    url: "sskj/basic/labor/engWorker/list",
  },
  pgList: {
    url: "sskj/basic/labor/engWorker/page",
  },
  engWorkerStatistical: {
    url: "sskj/basic/labor/engWorker/statistical/param",
    method: "GET",
  },
  personList: { url: "sskj/basic/labor/workOrderPerson/count" },
  pxjl: { url: "sskj/basic/labor/stbms/findTrainRecordByPerson" },
  pgjl: { url: "sskj/basic/labor/workOrderPerson/getList" },
  jcjl: { url: "sskj/basic/labor/engWorker/attendance/page", method: "GET" },
  gz: { url: "sskj/basic/wages/page" },
  wj: { url: "sskj/basic/fdfs/doclink/findbyglid" },
  // 获取考试试卷
  kssj: {
    url: "sskj/basic/labor/stbms/findByIdAndIdCardNumber",
    method: "GET",
  },
  //获取人员评价
  rypj: {
    url: "sskj/basic/labor/comment/page",
    method: "GET",
  },
  //获取复工工程数量
  map: {
    url: "sskj/basic/search/fg/map",
    method: "GET",
  },
});

// 项目中心信息查询
context.create("xmzx.info", {
  // 项目工程数量统计信息
  eq: {
    url: "sskj/xmzx/eq",
  },
  // 图审基础数据
  ts: {
    url: "sskj/xmzx/ts",
  },
  // 招投标数据
  ztb: {
    url: "sskj/xmzx/ztb",
  },
  // 施工合同数据
  sght: {
    url: "sskj/xmzx/sght",
  },
  // 违规处理数据分类
  wg: {
    url: "sskj/xmzx/wg",
  },
  // 施工许可数据分类
  xkz: {
    url: "sskj/xmzx/xkz",
  },
});

context.create("elastic.search", {
  dispatch: {
    url: "sskj/basic/cio/credit_level",
  },
  personcredit: {
    url: "sskj/basic/person/credit_level",
  },
});

//查询派工人口明细
context.create("elastic.search", {
  dispatchMx: {
    url: "/elastic_sskj/api/search/organ/gcpmazfws?",
  },
  personcredit: {
    url: "sskj/basic/person/credit_level",
  },
});

// 项目明细信息查询
context.create("xmmx.info", {
  // 项目基本信息查询
  jdgc: {
    url: "sskj/xmmx/jdgc",
    method: "GET",
  },
  sgxkgc: {
    url: "sskj/sgxk/sgxk",
    method: "GET",
  },
  ghxk: {
    url: "sskj/sgxk/ghxk",
    method: "GET",
  },
  sgxmb: {
    url: "sskj/sgxk/sgxmb",
    method: "GET",
  },
  jljg: {
    url: "sskj/sgxk/jljg",
    method: "GET",
  },
  zbgc: {
    url: "sskj/zb/zbgc",
    method: "GET",
  },
  zbdlry: {
    url: "sskj/zb/zbdlry",
    method: "GET",
  },
  zbjgry: {
    url: "sskj/zb/zbjgry",
    method: "GET",
  },
  ht: {
    url: "sskj/ht/ht",
    method: "GET",
  },
  htdxgc: {
    url: "sskj/ht/htdxgc",
    method: "GET",
  },
  ts: {
    url: "sskj/ts/ts",
    method: "GET",
  },
  xm: {
    url: "sskj/project/xm",
    method: "GET",
  },
  xmlxpw: {
    url: "sskj/project/lxpw",
    method: "GET",
  },
  xmzjly: {
    url: "sskj/project/xmzjly",
    method: "GET",
  },
  timeline: {
    url: "sskj/xmmx/timeline",
    method: "GET",
  },
  error: {
    url: "sskj/xmmx/error",
    method: "GET",
  },
  kqjl: {
    url: "sskj/xmmx/engkq",
    method: "GET",
  },
  cameraInfo: {
    url: "sskj/xmmx/camera",
    method: "GET",
  },
});

// 企业信息
context.create("org.info", {
  // 项目基本信息查询
  jbxx: {
    url: "sskj/org/jbxx",
    method: "GET",
  },
  timeline: {
    url: "sskj/org/timeline",
    method: "GET",
  },
  ryxx: {
    url: "sskj/org/ryxx",
    method: "GET",
  },
  zzxx: {
    url: "sskj/org/zzxx",
    method: "GET",
  },
  cxfxx: {
    url: "sskj/org/cxfxx",
    method: "GET",
  },
  zjxmxx: {
    url: "sskj/org/zjxmxx",
    method: "GET",
  },
  yjxmxx: {
    url: "sskj/org/yjxmxx",
    method: "GET",
  },
});

// 个人信息
context.create("emp.info", {
  // 项目基本信息查询
  jbxx: {
    url: "sskj/emp/jbxx",
    method: "GET",
  },
  zsxx: {
    url: "sskj/emp/zsxx",
    method: "GET",
  },
  timeline: {
    url: "sskj/emp/timeline",
    method: "GET",
  },
  zjxmxx: {
    url: "sskj/emp/zjxmxx",
    method: "GET",
  },
  yjxmxx: {
    url: "sskj/emp/yjxmxx",
    method: "GET",
  },
});

context.create("amap", {
  markers: {
    url: "sskj/amap/markers",
    method: "GET",
  },
  "yc.markers": {
    url: "sskj/amap/markers/ycmarkers",
    method: "GET",
  },
});

context.create("message", {
  search: {
    url: "sskj/message/page/s",
    method: "GET",
  },
});

context.create("ycjk", {
  // 设备实时数据
  edata: {
    url: "sskj/yc/edata/pcode",
    method: "GET",
  },
  // 工地设备PM曲线图数据查询
  edatas: {
    url: "sskj/yc/edatas",
    method: "GET",
  },
  // 工地设备PM排名
  pm10top10: {
    url: "sskj/yc/pm10top10",
    method: "GET",
  },
  // 预警统计
  "tj.aq": {
    url: "sskj/yc/tj/aq",
    method: "GET",
  },
  // 获取过去10天预警次数
  alertHistoryLastDay: {
    url: "sskj/yc/alertHistoryLastDay",
    method: "GET",
  },
  // 获取过去10天预警次数
  alertHistoryLastDay2: {
    url: "sskj/yc/alertHistoryLastDay2",
    method: "GET",
  },
  // 当日工程预警次数排名
  alertCountRank: {
    url: "sskj/yc/alertCountRank",
    method: "GET",
  },
  // 扬尘页面顶部统计数据（工程数量、设备数量、红色预警次数、黄色预警次数）
  topAlertCount: {
    url: "sskj/yc/getAlertCount",
    method: "GET",
  },
  // 获取已安装扬尘监控的工程数量
  engCount: {
    url: "sskj/yc/engCount",
    method: "GET",
  },
  // 获取扬尘监控设备总数
  equipmentCount: {
    url: "sskj/yc/equipmentCount",
    method: "GET",
  },
  // 获取扬尘监控黄色预警次数（当天）
  yellowAlertCount: {
    url: "sskj/yc/yellowAlertCount",
    method: "GET",
  },
  // 获取扬尘监控黄色预警次数（当天）
  redAlertCount: {
    url: "sskj/yc/redAlertCount",
    method: "GET",
  },
  // 获取天气数据
  queryWeatherInfo: {
    url: "sskj/yc/queryWeatherInfo",
    method: "GET",
  },
  // 获取摄像头信息
  queryCamera: {
    url: "sskj/yc/queryCamera",
    method: "GET",
  },
  allEquipmentInfo: {
    url: "sskj/yc/edata/page",
    method: "GET",
  },
  // 获取扬尘预警详细（当天）
  todayWorkInfoByEcode: {
    url: "sskj/yc/todayWorkInfoByEcode.do?",
    method: "GET",
  },
});

context.create("geocode", {
  geo: {
    urlPrefix: geoApiUrlPrefix,
    url: "geo",
    method: "GET",
    fit(response) {
      let ret = {
        success: response.status === "1",
      };
      if (ret.success) {
        if (response.hasOwnProperty("geocodes")) {
          ret.content = response.geocodes[0].location;
        } else {
          ret.content = null;
        }
      } else {
        ret.error = {
          message: "地理编码获取失败",
        };
      }
      return ret;
    },
  },
});

// 复工复产接口
context.create("fgfc", {
  // 获取返岗人员分布数据
  "canton.count": {
    url: "sskj/basic/search/fg/canton/count",
    method: "GET",
  },
  "stage.count": {
    url: "sskj/basic/search/fg/stage/count",
    method: "GET",
  },
  // 分页查询检测机构和商品砼已经审核通过可以开工的
  "stage.cio.page": {
    url: "sskj/basic/search/fg/stage/cio/page",
    method: "GET",
  },
  // 查询检测机构和商品砼的复工各阶段数据统计
  "stage.cio.count": {
    url: "sskj/basic/search/fg/stage/count/cio",
    method: "GET",
  },
  // 劳务实名制人员数量统计查询
  "labor.count": {
    url: "sskj/basic/search/fg/labor/count",
    method: "GET",
  },
  // 复工项目人员数量统计
  "xmry.count": {
    url: "sskj/basic/search/fg/xmry/count",
    method: "GET",
  },
  "jc.spt.cio.count": {
    url: "sskj/basic/search/fg/stage/cio/count",
    method: "GET",
  },
  "clock.count": {
    url: "sskj/basic/search/fg/clock/count",
    method: "GET",
  },
  // 复工审核通过项目
  "fg.eng.page": {
    url: "sskj/basic/search/fg/stage/eng/page",
    method: "GET",
  },
  "labor.page": {
    url: "sskj/basic/labor/page",
    method: "GET",
  },
  "labor.person.page": {
    url: "sskj/basic/labor/person/pageIndex",
    method: "GET",
  },
});

export default context.api;
